<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-12-01 15:50
 *
 * Version: 2.0
 */

namespace App\Modules\Gift\Services;


use App\Exceptions\AllValidatorException;
use App\Exceptions\DbException;
use App\Modules\Employee\Repositories\EmployeeRepository;
use App\Modules\Gift\Repositories\GiftRepository;
use App\Modules\Gift\Repositories\GiftSendRepository;
use App\Modules\Live\Repositories\LiveRepository;
use App\Modules\Member\Repositoryies\MemberRepository;
use Flower\Core\Repository\Eloquent\BaseService;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class GiftService extends BaseService
{

	private  $giftRepository;
	private $memberRepository;
	private $giftSendRepository;
	private $employeeRepository;
	private $liveRepository;
	public function __construct(GiftRepository $giftRepository,MemberRepository $memberRepository,GiftSendRepository $giftSendRepository,EmployeeRepository $employeeRepository,LiveRepository $liveRepository)
	{
		$this->giftRepository = $giftRepository;
		$this->memberRepository = $memberRepository;
		$this->giftSendRepository = $giftSendRepository;
		$this->employeeRepository = $employeeRepository;
		$this->liveRepository = $liveRepository;
	}


	public function lst(){
		$list =  $this->giftRepository->getList();
		$result = array_map(function($v){
			$v['money'] = intval($v['money']);
			return $v;
		},$list->toArray());
		return ['status'=>1,'msg'=>'礼物列表','data'=>$result];
	}

	/**
	 * 检测是否能发礼物
	 * @param $giftId
	 * @param $memberId
	 * @return array
	 * @throws \App\Exceptions\NotFoundException
	 */
	public function checkMemberCanSendGift($giftId,$memberId){
		$list = $this->checkMemberAndGiftIsExisted($giftId,$memberId);
		list($giftInfo,$memberInfo)=array_values($list);
		$result = ['status'=>1,'msg'=>'可以购买','data'=>''];
		if($memberInfo->coin < $giftInfo->money){
			$result = ['status'=>0,'msg'=>'县令币不足,请充值!','data'=>''];
		}
		return $result;
	}

	/**
	 * 检测礼物和用户是否存在
	 * @param $giftId
	 * @param $memberId
	 * @return array
	 * @throws \App\Exceptions\NotFoundException
	 */
	protected function checkMemberAndGiftIsExisted($giftId,$memberId){
		$giftInfo = $this->giftRepository->isExisted($giftId);
		$memberInfo =$this->memberRepository->isExisted($memberId);
		if ($memberInfo->coin >= $giftInfo->money){
			return compact('giftInfo','memberInfo');
		}else{
			throw new AllValidatorException(['msg'=>'县令币不足，不能发送礼物！']);
		}
	}

	/**
	 * 发送礼物
	 * @param $giftId
	 * @param $memberId
	 * @param $userId
	 * @param $liveId
	 * @return array
	 * @throws DbException
	 * @throws \App\Exceptions\AllValidatorException
	 * @throws \App\Exceptions\NotFoundException
	 */
	public function sendGiftToBarber($giftId,$memberId,$userId,$liveId){
		$liveInfo = $this->liveRepository->checkExist($liveId);
		$list = $this->checkMemberAndGiftIsExisted($giftId,$memberId);
		list($giftInfo,$memberInfo)=array_values($list);
		$userInfo = $this->employeeRepository->isExited($userId);
		DB::beginTransaction();
		try{
			$this->giftSendRepository->add($giftId,$memberId,$userId,$giftInfo->money,$liveId);
			$memberInfo->coin -= $giftInfo->money;
			$memberInfo->save();
			$userInfo->coin += $giftInfo->money;
			$userInfo->save();
			DB::commit();
			return ['status'=>1,'msg'=>'发送礼物成功！','data'=>$giftInfo];
		}catch (DbException $e){
			Log::error('发送礼物时异常',['memberInfo'=>$memberInfo,'error_msg'=>$e->getMessage(),'error_code'=>$e->errorCode]);
			DB::rollBack();
			throw new DbException();
		}
	}


	/**
	 * 礼物列表
	 * @param $userId
	 * @param $liveId
	 * @return array
	 * @throws \App\Exceptions\AllValidatorException
	 */
	public function getListByBarberId($userId,$liveId){
		$this->liveRepository->checkExist($liveId);
		$list = $this->giftSendRepository->lst($userId,$liveId);
		return ['status'=>1,'msg'=>'礼物列表','data'=>$list];
	}
}